package com.edu.dreams.controller;

import com.edu.dreams.entity.Anim;
import com.edu.dreams.entity.News;
import com.edu.dreams.mapper.NewsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.UUID;

//@Controller注解
@Controller

public class NewsController {

	@Autowired
	NewsMapper newsMapper;

	@RequestMapping("news")
	public String news(Model model) {
		model.addAttribute("newsdata", newsMapper.getAllNews());
		return "news";
	}

	@PostMapping("addNews")
	@ResponseBody
	public String addNews(HttpServletRequest request, News news, @RequestParam("picfile") MultipartFile picfile) {
		int res = 0;
		try {
			if (!picfile.isEmpty()) {
				String filename = picfile.getOriginalFilename();
				String basepath = request.getServletContext().getRealPath("/upload/");
				File filePath = new File(basepath);
				if (!filePath.exists()) {
					filePath.mkdirs();
				}
				String newFileName = UUID.randomUUID() + "_" + filename;
				picfile.transferTo(new File(basepath + newFileName));
				news.setNews_picurl(newFileName);
				res = newsMapper.addNews(news);
			} else {
				return "<script>alert('添加失败!文件上传失败！');window.location.href='seenew';</script>";
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "<script>alert('添加失败!');window.location.href='seenew';</script>";
		}
		return "<script>alert('" + ((res == 1) ? "添加成功！" : "添加失败！") + "');window.location.href='seenew';</script>";
	}

	@PostMapping("updatenews")
	@ResponseBody
	public String updateNews(HttpServletRequest request, News news, @RequestParam("picfile") MultipartFile picfile) {
		int res = 0;
		try {
			News news1 = newsMapper.getNewsById(news.getNews_id());
			if (!news1.getNews_picurl().contains(picfile.getOriginalFilename())) {
				if (!picfile.isEmpty()) {
					String basepath = request.getServletContext().getRealPath("/upload/");
					File filePath = new File(basepath);
					if (!filePath.exists()) {
						filePath.mkdirs();
					}
					String newFileName = UUID.randomUUID() + "_" + picfile.getOriginalFilename();
					picfile.transferTo(new File(basepath + newFileName));
					news.setNews_picurl(newFileName);
				} else {
					return "<script>alert('更新失败!文件上传失败！');window.location.href='seenew';</script>";
				}
			} else {
				news.setNews_picurl(news1.getNews_picurl());
			}
			res = newsMapper.updateNews(news);
		} catch (Exception e) {
			e.printStackTrace();
			return "<script>alert('更新失败!');window.location.href='seenew';</script>";
		}
		return "<script>alert('" + ((res == 1) ? "更新成功！" : "更新失败！") + "');window.location.href='seenew';</script>";
	}

	@RequestMapping("deletenews")
	@ResponseBody
	public String deleteNews(int id) {
		int res = newsMapper.deleteNews(id);
		return "<script>alert('" + ((res == 1) ? "删除成功！" : "删除失败！") + "');window.location.href='seenew';</script>";
	}

	@RequestMapping("newsshow")
	public String newsShow(Integer id, Model model) {
		if (id == null) {
			return "redirect:news";
		}
		News news = newsMapper.getNewsById(id);
		model.addAttribute("newdata", news);
		model.addAttribute("newsdata", newsMapper.getAllNews());
		return "newsshow";
	}

	@PostMapping("searchNews")
	@ResponseBody
	public String searchNews(HttpServletRequest request, String animname) {
		if (StringUtils.isEmpty(animname)) {
			return "<script>alert('搜索内容不能为空！');window.location.href='news';</script>";
		} else {
			return "<script>window.location.href='" + request.getContextPath() + "/search?aname=" + animname
					+ "&type=2';</script>";
		}
	}
}
